/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * File: Speed_Control.c
 *
 * Code generated for Simulink model 'SMO_Arctan_FOC'.
 *
 * Model version                  : 2.7
 * Simulink Coder version         : 9.8 (R2022b) 13-May-2022
 * C/C++ source code generated on : Fri Oct 11 14:25:16 2024
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Speed_Control.h"
#include "SMO_Arctan_FOC.h"
#include "rtwtypes.h"

/* System initialize for function-call system: '<S1>/Speed_Control' */
void SMO_Arctan_F_Speed_Control_Init(void)
{
  /* SystemInitialize for Enabled SubSystem: '<S4>/Subsystem' */
  /* SystemInitialize for SignalConversion generated from: '<S300>/Id_Ref' */
  SMO_Arctan_FOC_B.OutportBufferForId_Ref = SMO_Arctan_FOC_ConstB.Constant;

  /* End of SystemInitialize for SubSystem: '<S4>/Subsystem' */
}

/* Output and update for function-call system: '<S1>/Speed_Control' */
void SMO_Arctan_FOC_Speed_Control(void)
{
  /* Outputs for Enabled SubSystem: '<S4>/Subsystem' incorporates:
   *  EnablePort: '<S300>/Enable'
   */
  /* RelationalOperator: '<S299>/Compare' incorporates:
   *  Constant: '<S299>/Constant'
   */
  if (SMO_Arctan_FOC_B.Motor_State == 2.0) {
    real32_T rtb_Sum;
    uint32_T Subsystem_ELAPS_T;
    if (!SMO_Arctan_FOC_DW.Subsystem_MODE) {
      SMO_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = true;

      /* Enable for DiscreteIntegrator: '<S333>/Integrator' */
      SMO_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 1U;
      SMO_Arctan_FOC_DW.Subsystem_MODE = true;
    }

    if (SMO_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T) {
      Subsystem_ELAPS_T = 0U;
    } else {
      Subsystem_ELAPS_T = SMO_Arctan_FOC_M->Timing.clockTick0 -
        SMO_Arctan_FOC_DW.Subsystem_PREV_T;
    }

    SMO_Arctan_FOC_DW.Subsystem_PREV_T = SMO_Arctan_FOC_M->Timing.clockTick0;
    SMO_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = false;

    /* SignalConversion generated from: '<S300>/Id_Ref' */
    SMO_Arctan_FOC_B.OutportBufferForId_Ref = SMO_Arctan_FOC_ConstB.Constant;

    /* Sum: '<S300>/Sum' incorporates:
     *  Inport: '<Root>/Target_Speed'
     */
    rtb_Sum = SMO_Arctan_FOC_U.target_speed - SMO_Arctan_FOC_B.Delay;

    /* DiscreteIntegrator: '<S333>/Integrator' */
    if (SMO_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h == 0) {
      /* DiscreteIntegrator: '<S333>/Integrator' */
      SMO_Arctan_FOC_DW.Integrator_DSTATE_c += 5.0E-5F * (real32_T)
        Subsystem_ELAPS_T * SMO_Arctan_FOC_DW.Integrator_PREV_U_a;
    }

    /* End of DiscreteIntegrator: '<S333>/Integrator' */

    /* Sum: '<S342>/Sum' incorporates:
     *  Gain: '<S338>/Proportional Gain'
     */
    SMO_Arctan_FOC_B.Saturation = 0.000264109141F * rtb_Sum +
      SMO_Arctan_FOC_DW.Integrator_DSTATE_c;

    /* Saturate: '<S340>/Saturation' */
    if (SMO_Arctan_FOC_B.Saturation > 3.26732683F) {
      /* Sum: '<S342>/Sum' incorporates:
       *  Saturate: '<S340>/Saturation'
       */
      SMO_Arctan_FOC_B.Saturation = 3.26732683F;
    } else if (SMO_Arctan_FOC_B.Saturation < -3.26732683F) {
      /* Sum: '<S342>/Sum' incorporates:
       *  Saturate: '<S340>/Saturation'
       */
      SMO_Arctan_FOC_B.Saturation = -3.26732683F;
    }

    /* End of Saturate: '<S340>/Saturation' */

    /* Update for DiscreteIntegrator: '<S333>/Integrator' incorporates:
     *  Gain: '<S330>/Integral Gain'
     */
    SMO_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 0U;
    SMO_Arctan_FOC_DW.Integrator_PREV_U_a = 0.00417593215F * rtb_Sum;
  } else {
    SMO_Arctan_FOC_DW.Subsystem_MODE = false;
  }

  /* End of RelationalOperator: '<S299>/Compare' */
  /* End of Outputs for SubSystem: '<S4>/Subsystem' */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
